// pages/report/search_res/searchc_res.js
const api=require('../../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareModel:{
        title:'',
        path:'pages/test/test', 
        fromButton:'one'
      },  
    tabNavActive:0,
    tabNav:["报告","图表"],
    FX:false,
    pageNum:1,
    pageSize:10,
    total: 0,
    pages:0,
    recommendData:[
    
    ],
    searchInput:'',
    industryId:'',
    noData: false,
    gdLoading: true,
    posterId:'',
    name:'',
    image:''
  },
    //分享
    fxWrap(e){ 
       this.setData({
        FX:true,
        posterId: e.currentTarget.dataset.id,
        name: e.currentTarget.dataset.name,
        image: e.currentTarget.dataset.image
      })
       
    },
//取消
cancleBack(){
   this.setData({
    searchInput:'',
    pageNum: 1
   })
   if(this.data.tabNavActive===0){
    this.loadData(1)
   }else{
       this.imageLists(1)
   }
  
},
save_report(){
    let userInfo= wx.getStorageSync('user')
    if(userInfo!==''){
        let user= JSON.parse(userInfo) 
        let reportId=this.data.posterId
         api.post('weChatMini/saveCollect',{memberId: user.id,reportId:reportId}).then(res=>{
            if(res.code===200){
                wx.showToast({
                    title: '收藏成功',
                    icon: 'none',
                    duration: 2000,
                  })
                  
            }
        })
    }else{
        wx.showToast({
            title: '请登录',
            icon: 'none',
            duration: 2000,
          })
    }
   
    
  },
fxCancel(){
  this.setData({
    FX:false,
  });
},
tabNavClick(e){  
    this.setData({
        recommendData:[],
        pages: 0,
        pageNum: 1
    })    
   this.setData({
    tabNavActive:e.currentTarget.dataset.id
  });
  if(e.currentTarget.dataset.id==0){
     this.loadData(1)
  }else{
     this.imageLists(1)
  }
},
imageLists(type){
    let that=this
     
    api.post('web/imageLists',{pageNum:this.data.pageNum,pageSize:this.data.pageSize,name:this.data.searchInput}).then(res=>{
        if(res.code===200){
            
             if(type===2){
                let data=that.data.recommendData.concat(res.data.records)
                that.setData({
                    recommendData:data,
                    pages: res.data.pages,
                    
                })    
              }else{
                    that.setData({
                        recommendData:res.data.records,
                        pages: res.data.pages
                    })
                }
          
        }
    })
},
bindconfirm(){
     
    this.setData({
        pageNum: 1
    })
    if(this.data.tabNavActive===0){
        this.loadData(1)
       }else{
        this.imageLists(1)
       }
},
search(e){
     this.setData({
        searchInput: e.detail.value 
    })
},
changeData(){
   debugger  
   const tabNavActive= this.data.tabNavActive
    if(this.data.pages>this.data.pageNum){
       this.setData({
           pageNum: this.data.pageNum+1
       })
       if(tabNavActive===0){
        this.loadData(2)
       }else{
           this.imageLists(2)
       }
       
   }else{
      this.setData({
        gdLoading: false,
        noData: true
      })
   }
  },
searchLoad(){
    this.setData({
        pageNum: 1
    })
    if(this.data.tabNavActive===0){
        this.loadData(1)
    }else{
        this.imageLists(1)
    }
   
},
loadData(type){
    let that=this
    let param={searchName:this.data.searchInput,pageNum:this.data.pageNum,pageSize: this.data.pageSize}
    api.post('weChatMini/reportList',param).then(res=>{
        console.log('res',res)
        if(res.code===200){
            res.data.records.forEach(item=>{
                item.isSelected=true
            })
            if(type===2){
                let data=that.data.recommendData.concat(res.data.records)
                that.setData({
                    recommendData:data,
                    pages: res.data.pages,
                    
                })    
              }else{
                    that.setData({
                        recommendData:res.data.records,
                        pages: res.data.pages
                    })
                }
          
        }
    }) 
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options',options)
    this.setData({
        searchInput: options.searchInput,
        industryId:options.options
    })
    this.loadData(1)
  },
  zan(e){
    let id=e.currentTarget.id
    let index= e.currentTarget.dataset.index 
    let data=this.data.recommendData
    let that=this
    let fasg=data[index].isSelected
    if(data[index].isSelected){
        data[index].browseNum=data[index].browseNum+1
    }else{
        data[index].browseNum=data[index].browseNum-1 
    }
    data[index].isSelected=!data[index].isSelected
    api.post('weChatMini/saveRecommend',{id: id ,isSelected: fasg}).then(res=>{
        if(res.code===200){
            that.setData({
                recommendData: data
            })
        }
    })
    
  },
  changePdf(e){
    console.log('e',e)
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
        // 普通页面
        url:'../pdf/pdf?id='+id
      })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { 
      debugger
    this.changeData()
  },

  copy(data){
    let reportId= this.data.posterId 
    wx.setClipboardData({
      data: 'https://www.xinyanbao.cn/#/pdf/?id='+reportId,
      success:function(res){ 
      }
    })
 },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log('res',res)
    if(res.from==='button'){
        this.setData({
            FX: false
        }) 
       return {
        title: this.data.name,
        path:'/pages/report/pdf/pdf?id='+this.data.posterId,
        imageUrl: this.data.image
      }
    }
 
},

   //保存海报
   save_report(){
    this.setData({
        FX: false
    })
  wx.navigateTo({
    url: '../save_report/save_report?posterId='+this.data.posterId,
  })
},
})